﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using HouseSecretary.Model;
using DevExpress.XtraEditors.Controls;

namespace HouseSecretary.HouseForm
{
    public partial class HouseSrcInfoWebDCForm : Base.MdiChildForm
    {
        /*下拉框默认条数*/
        int ComboMaxCount = 8;


        public HouseSrcInfoWebDCForm()
        {
            InitializeComponent();
        }

        protected override void InitToolButtons()
        {
            //base.InitToolButtons();
            List<Model.Buttons> buttonList = new List<Model.Buttons>();
            Model.Buttons buttonsModel;

            //buttonsModel = new Model.Buttons();
            //buttonsModel.BtnCaption = "&Q.房源查询";
            //buttonsModel.BtnType = Model.ButtonType.queryAdvance;
            //buttonList.Add(buttonsModel);

            buttonsModel = new Model.Buttons();
            buttonsModel.BtnCaption = "&R.刷新";
            buttonsModel.BtnType = Model.ButtonType.refresh;
            buttonList.Add(buttonsModel);

            //buttonsModel = new Model.Buttons();
            //buttonsModel.BtnCaption = "&A.入库";
            //buttonsModel.BtnType = Model.ButtonType.add;
            //buttonList.Add(buttonsModel);

            buttonsModel = new Model.Buttons();
            buttonsModel.BtnCaption = "&L.查看";
            buttonsModel.BtnType = Model.ButtonType.look;
            buttonList.Add(buttonsModel);

            buttonsModel = new Model.Buttons();
            buttonsModel.BtnCaption = "&C.关闭";
            buttonsModel.BtnType = Model.ButtonType.close;
            buttonList.Add(buttonsModel);

            this.Tag = buttonList;
        }

        private void HouseSrcInfoWebDCForm_Load(object sender, EventArgs e)
        {
            InitComponents();
            BindPageGridList("");
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void ButtonLook(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            HouseSrcLook();
        }
        /// <summary>
        /// 双击查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvHouseSrc_DoubleClick(object sender, EventArgs e)
        {
            HouseSrcLook();
        }
        /// <summary>
        /// 查看
        /// </summary>
        private void HouseSrcLook()
        {
            if (gvHouseSrc.FocusedRowHandle > -1)
            {
                Model.CommonDlgParameters CommonDlgParametersModel = new Model.CommonDlgParameters();
                CommonDlgParametersModel.ActionMethod = Model.ActionMethodType.Query;
                CommonDlgParametersModel.StringList.Add(gvHouseSrc.GetFocusedRowCellValue("FHouseSrcInfoID").ToString());
                HouseSrcInfoWebDCLookDlgForm houseSrcInfoWebDCLookDlgForm = new HouseSrcInfoWebDCLookDlgForm(CommonDlgParametersModel);
                houseSrcInfoWebDCLookDlgForm.ShowDialog();
                BindPageGridList("");
            }
            
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void ButtonClose(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        #region 分页绑定

        //页行数 
        public int pagesize = 10; 
        //当前页 
        public int pageIndex = 1; 
        //总页数 
        public int pageCount; 


        /// <summary> 
        /// 绑定分页控件和GridControl数据 
        /// </summary> 
        /// <creator>sunlj</creator> 
        /// <date>2015-02-09</date> 
        /// <param name="strWhere">查询条件</param> 
        public void BindPageGridList(string strWhere) 
        {
            nvgtDataPager.Buttons.CustomButtons[0].Enabled = true;
            nvgtDataPager.Buttons.CustomButtons[1].Enabled = true;
            nvgtDataPager.Buttons.CustomButtons[2].Enabled = true;
            nvgtDataPager.Buttons.CustomButtons[3].Enabled = true;
            int row = 99;
            //获取总页数 
            if (row % pagesize > 0) 
            { 
                pageCount = row / pagesize + 1; 
            } 
            else 
            { 
                pageCount = row / pagesize; 
            }
            if (pageIndex == 1)
            {
                nvgtDataPager.Buttons.CustomButtons[0].Enabled = false;
                nvgtDataPager.Buttons.CustomButtons[1].Enabled = false; ;
            }
            //最后页时获取真实记录数 
            if (pageCount == pageIndex)
            {
                nvgtDataPager.Buttons.CustomButtons[2].Enabled = false;
                nvgtDataPager.Buttons.CustomButtons[3].Enabled = false;
            } 
            //分页获取数据列表 

            string strJson = "[";
            strJson += "{'FHouseSrcInfoID':'1','FTitle':'标题','FDCSource':'58同城','FRentSale':'S','FDistrictNo4':'330203','FAddr':'天翼好景','HasImage':'Y','FArea':'50','DoorModel':'2室1厅1卫','FDCDate':'2015-02-09 09:48:07.780','FPrice':'600000','FStatus':'0','FFloor':'3','FTotalFloor':'5','FLookCnt':'0'},";
            strJson += "{'FHouseSrcInfoID':'2','FTitle':'标题','FDCSource':'58同城','FRentSale':'S','FDistrictNo4':'330203','FAddr':'天翼好景','HasImage':'Y','FArea':'50','DoorModel':'2室1厅1卫','FDCDate':'2015-02-09 09:48:07.780','FPrice':'600000','FStatus':'0','FFloor':'3','FTotalFloor':'5','FLookCnt':'0'},";
            strJson += "{'FHouseSrcInfoID':'3','FTitle':'标题','FDCSource':'58同城','FRentSale':'S','FDistrictNo4':'330203','FAddr':'天翼好景','HasImage':'Y','FArea':'50','DoorModel':'2室1厅1卫','FDCDate':'2015-02-09 09:48:07.780','FPrice':'600000','FStatus':'0','FFloor':'3','FTotalFloor':'5','FLookCnt':'0'},";
            strJson += "{'FHouseSrcInfoID':'4','FTitle':'标题','FDCSource':'58同城','FRentSale':'S','FDistrictNo4':'330203','FAddr':'天翼好景','HasImage':'Y','FArea':'50','DoorModel':'2室1厅1卫','FDCDate':'2015-02-09 09:48:07.780','FPrice':'600000','FStatus':'0','FFloor':'3','FTotalFloor':'5','FLookCnt':'0'},";
            strJson += "{'FHouseSrcInfoID':'5','FTitle':'标题','FDCSource':'58同城','FRentSale':'S','FDistrictNo4':'330203','FAddr':'天翼好景','HasImage':'N','FArea':'50','DoorModel':'2室1厅1卫','FDCDate':'2015-02-09 09:48:07.780','FPrice':'600000','FStatus':'0','FFloor':'3','FTotalFloor':'5','FLookCnt':'0'},";
            strJson += "{'FHouseSrcInfoID':'6','FTitle':'标题','FDCSource':'58同城','FRentSale':'S','FDistrictNo4':'330203','FAddr':'天翼好景','HasImage':'N','FArea':'50','DoorModel':'2室1厅1卫','FDCDate':'2015-02-09 09:48:07.780','FPrice':'600000','FStatus':'0','FFloor':'3','FTotalFloor':'5','FLookCnt':'0'},";
            strJson += "{'FHouseSrcInfoID':'7','FTitle':'标题','FDCSource':'58同城','FRentSale':'S','FDistrictNo4':'330203','FAddr':'天翼好景','HasImage':'Y','FArea':'50','DoorModel':'2室1厅1卫','FDCDate':'2015-02-09 09:48:07.780','FPrice':'600000','FStatus':'0','FFloor':'3','FTotalFloor':'5','FLookCnt':'0'},";
            strJson += "{'FHouseSrcInfoID':'8','FTitle':'标题','FDCSource':'58同城','FRentSale':'S','FDistrictNo4':'330203','FAddr':'天翼好景','HasImage':'Y','FArea':'50','DoorModel':'2室1厅1卫','FDCDate':'2015-02-09 09:48:07.780','FPrice':'600000','FStatus':'0','FFloor':'3','FTotalFloor':'5','FLookCnt':'0'},";
            strJson += "{'FHouseSrcInfoID':'9','FTitle':'标题','FDCSource':'58同城','FRentSale':'S','FDistrictNo4':'330203','FAddr':'天翼好景','HasImage':'Y','FArea':'50','DoorModel':'2室1厅1卫','FDCDate':'2015-02-09 09:48:07.780','FPrice':'600000','FStatus':'0','FFloor':'3','FTotalFloor':'5','FLookCnt':'0'},";
            strJson += "{'FHouseSrcInfoID':'10','FTitle':'标题','FDCSource':'58同城','FRentSale':'S','FDistrictNo4':'330203','FAddr':'天翼好景','HasImage':'Y','FArea':'50','DoorModel':'2室1厅1卫','FDCDate':'2015-02-09 09:48:07.780','FPrice':'600000','FStatus':'0','FFloor':'3','FTotalFloor':'5','FLookCnt':'0'},";
            strJson = strJson.Trim(',');
            strJson += "]";

            List<Model.HouseSrcInfoWebDC> HouseSrcList = JsonConvert.DeserializeObject<List<Model.HouseSrcInfoWebDC>>(strJson);

            dgrdHouseSrc.DataSource = HouseSrcList;
            nvgtDataPager.DataSource = HouseSrcList;
            nvgtDataPager.TextStringFormat = string.Format("第 {0}页, 共 {1}页", pageIndex, pageCount); 
        }

        


        /// <summary> 
        /// 按钮点击事件 
        /// </summary> 
        /// <creator>sunlj</creator> 
        /// <date>2015-02-09</date> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        private void nvgtDataPager_ButtonClick(object sender, NavigatorButtonClickEventArgs e) 
        { 
            ShowEvent("ButtonClick", e.Button); 
        } 
        /// <summary> 
        /// 分页事件处理 
        /// </summary> 
        /// <param name="eventString">事件名称</param> 
        /// <param name="button">按钮控件</param> 
        /// <creator>sunlj</creator> 
        /// <date>2015-02-09</date> 
        private void ShowEvent(string eventString, NavigatorButtonBase button) 
        {
            //string type = button.ButtonType.ToString(); 
            NavigatorCustomButton btn = (NavigatorCustomButton)button;
            int buttonIndex = btn.Index;
            if (buttonIndex == 1) { pageIndex = 1; } //首页
            if (buttonIndex == 3) { pageIndex++; } //下一页
            if (buttonIndex == 4) { pageIndex = pageCount; } //末页
            if (buttonIndex == 2) { pageIndex--; } //上一页
            //绑定分页控件和GridControl数据 
            BindPageGridList(""); 
        }

        

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitComponents()
        {
            //隐藏最上面的GroupPanel
            this.gvHouseSrc.OptionsView.ShowGroupPanel = false;
            //设置成一次选择一行，并且不能被编辑
            this.gvHouseSrc.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            this.gvHouseSrc.OptionsBehavior.Editable = false;
            this.gvHouseSrc.OptionsSelection.EnableAppearanceFocusedCell = false;
            //设置显示横向滚动条
            this.gvHouseSrc.OptionsView.ColumnAutoWidth = false;
            this.gvHouseSrc.OptionsView.ShowFooter = true;

            for (int i = 1; i < gvHouseSrc.Columns.Count; i++)
            {
                gvHouseSrc.Columns[i].OptionsColumn.AllowEdit = false;
            }

            //租售类型
            List<Model.SysComboControl> comboList = new List<Model.SysComboControl>();
            Model.SysComboControl combo = new Model.SysComboControl();
            combo = new Model.SysComboControl();
            combo.Value = "C";
            combo.Name = "合租";
            comboList.Add(combo);
            combo = new Model.SysComboControl();
            combo.Value = "R";
            combo.Name = "出租";
            comboList.Add(combo);
            combo = new Model.SysComboControl();
            combo.Value = "S";
            combo.Name = "出售";
            comboList.Add(combo);
            RepositoryItemLookUpEdit columnEditor = new RepositoryItemLookUpEdit();
            columnEditor.DataSource = comboList;
            columnEditor.DisplayMember = "Name";
            columnEditor.ValueMember = "Value";
            columnEditor.NullText = "";
            dgrdHouseSrc.RepositoryItems.Add(columnEditor);
            gvHouseSrc.Columns["FRentSale"].ColumnEdit = columnEditor;

            //状态
            comboList = new List<Model.SysComboControl>();
            combo = new Model.SysComboControl();
            combo = new Model.SysComboControl();
            combo.Value = "0";
            combo.Name = "新";
            comboList.Add(combo);
            combo = new Model.SysComboControl();
            combo.Value = "1";
            combo.Name = "查看";
            comboList.Add(combo);
            combo = new Model.SysComboControl();
            combo.Value = "2";
            combo.Name = "入库";
            comboList.Add(combo);
            columnEditor = new RepositoryItemLookUpEdit();
            columnEditor.DataSource = comboList;
            columnEditor.DisplayMember = "Name";
            columnEditor.ValueMember = "Value";
            columnEditor.NullText = "";
            dgrdHouseSrc.RepositoryItems.Add(columnEditor);
            gvHouseSrc.Columns["FStatus"].ColumnEdit = columnEditor;



            //分页控件
            //nvgtDataPager.Buttons.ImageList = imgCollectForm;

            NavigatorCustomButton navigatorBtn= nvgtDataPager.Buttons.CustomButtons.Add();
            navigatorBtn.ImageIndex = 1;
            navigatorBtn.Index = 1;
            navigatorBtn = nvgtDataPager.Buttons.CustomButtons.Add();
            navigatorBtn.ImageIndex = 2;
            navigatorBtn.Index = 2;
            navigatorBtn = nvgtDataPager.Buttons.CustomButtons.Add();
            navigatorBtn.ImageIndex = 3;
            navigatorBtn.Index = 3;
            navigatorBtn = nvgtDataPager.Buttons.CustomButtons.Add();
            navigatorBtn.ImageIndex = 4;
            navigatorBtn.Index = 4;

            //页行数
            List<SysComboControl> pageSizeList = new List<SysComboControl>();
            SysComboControl sysCombo = new SysComboControl(); sysCombo.Name = "20"; sysCombo.Value = "0";
            pageSizeList.Add(sysCombo);
            sysCombo = new SysComboControl(); sysCombo.Name = "50"; sysCombo.Value = "1";
            pageSizeList.Add(sysCombo);
            sysCombo = new SysComboControl(); sysCombo.Name = "100"; sysCombo.Value = "2";
            pageSizeList.Add(sysCombo);
            cboFPageSize.Properties.DataSource = pageSizeList;
            cboFPageSize.Properties.DisplayMember = "Name";
            cboFPageSize.Properties.ValueMember = "Value";
            cboFPageSize.Properties.Columns.Add(new LookUpColumnInfo("Name", 0));
            cboFPageSize.Properties.PopupFormMinSize = new Size(10 * (cboFPageSize.Width) / (10 + 20), 0);
            if (pageSizeList.Count > ComboMaxCount)
            {
                cboFPageSize.Properties.DropDownRows = ComboMaxCount;
            }
            else
            {
                cboFPageSize.Properties.DropDownRows = pageSizeList.Count;
            }
            this.cboFPageSize.EditValue = "0";

            ////图
            //RepositoryItemImageComboBox imageCombo = dgrdHouseSrc.RepositoryItems.Add("ImageComboBoxEdit") as RepositoryItemImageComboBox;

            //DevExpress.Utils.ImageCollection images = new DevExpress.Utils.ImageCollection();
            //images.AddImage(Properties.Resources.start_hover);
            //imageCombo.SmallImages = images;
            //imageCombo.Items.Add(new ImageComboBoxItem("有图", 1, 0));
            //imageCombo.GlyphAlignment = DevExpress.Utils.HorzAlignment.Center;
            //column = gvHouseSrc.Columns["hasImage"];
            //column.ColumnEdit = imageCombo;
            //column.Caption += " (ImageComboBox)";
            //column.ShowButtonMode = DevExpress.XtraGrid.Views.Base.ShowButtonModeEnum.ShowOnlyInEditor;



            


        }

        private void cboFPageSize_EditValueChanged(object sender, EventArgs e)
        {
            this.pagesize = int.Parse(this.cboFPageSize.Text.Trim());

            BindPageGridList("");
        }
        #endregion








    }
}
